package com.caocaog.market.controller;

import com.caocaog.market.entity.ShopFareConfig;
import com.caocaog.market.enums.ShopFareConfigEnum;
import com.caocaog.market.service.ShopFareConfigService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.base.entity.groups.InsertGroup;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺加价配置
 *
 * @author chechen
 * @date 2022-10-29
 */
@Slf4j
@RestController
@RequestMapping("/shopFareConfig")
public class ShopFareConfigController extends GenericController<ShopFareConfig, ShopFareConfigService> {

    public static final String TABLE_MEANING = "店铺加价配置";


    @GetMapping(value = "/findByShopId")
    public Result<ShopFareConfig> findByShopId(@RequestParam String shopId) {
        return Result.OK(super.service.findShopFareConfigByShopId(shopId));
    }


    @AutoLog(value = "店铺加价配置-添加")
    @PostMapping(value = "/add")
    public Result<Integer> add(@RequestBody @Validated({InsertGroup.class}) ShopFareConfig shopFareConfig) {
        if (super.service.addShopFareConfig(shopFareConfig) > 0)
            return Result.OK(ADD_SUCCESS, 1);
        else return Result.error("新增失败", 0);
    }


    @AutoLog(value = "店铺加价配置-编辑")
    @PutMapping(value = "/edit")
    public Result<Integer> edit(@RequestBody @Validated({UpdateGroup.class}) ShopFareConfig shopFareConfig) {
        if (super.service.updateShopFareConfig(shopFareConfig) > 0)
            return Result.OK(EDIT_SUCCESS, 1);
        else return Result.error("编辑失败", 0);
    }


    @AutoLog(value = "店铺加价配置-变更状态")
    @PutMapping(value = "/changeState")
    public Result<Integer> changeState(@RequestParam String shopId, @RequestParam int shopFareConfigType) {
        if (super.service.changeShopFareConfigState(shopId, ShopFareConfigEnum.match(shopFareConfigType)) > 0)
            return Result.OK("变更状态成功", 1);
        else return Result.error("变更状态失败", 0);
    }


}
